# -*- ruby -*-

website.config['website.base_url'] = 'http://kramdown.gettalong.org/'
website.config['sources'] =[['/', :file_system, 'doc']]
website.config['destination'] = [:file_system, 'htmldoc']
website.config['website.tmpdir'] = 'webgen-tmp'

####
# tag options
website.config.define_option('tag.options.items', [])
website.ext.tag.register('options', :mandatory => ['items'],
                         :config_prefix => 'tag.options') do |tag, body, context|

  options = context[:config]['tag.options.items']
  options = if options == 'all'
              Kramdown::Options.definitions.sort.map {|k,v| v}
            else
              options.collect {|opt| Kramdown::Options.definitions[opt.intern]}
            end

  options.collect do |term|
    lines = term.desc.split(/\n/)
    first = lines.shift
    rest = lines[0..-2].collect {|l| "  " + l}.join("\n") << "\\\\\n  #{lines[-1]}"
    "`#{term.name}`{:\#option-#{term.name.to_s.tr('_', '-')}}\n: #{first}\n#{rest}\n\n"
  end.join("\n")
end


####
# tag kdexample
require 'cgi'
website.ext.tag.register('kdexample', :config_prefix => 'tag.kdexample') do |tag, body, context|
  body.strip!
  result = ::Kramdown::Document.new(body, :auto_ids => false).to_html
  before = "<pre class='kdexample-before'><code>#{CGI::escapeHTML(body)}\n</code></pre>"
  after = "<pre class='kdexample-after-source'><code>#{CGI::escapeHTML(result)}\n</code></pre>"
  afterhtml = "<div class='kdexample-after-live'>#{result}</div>"

  "<table class='kdexample'><tr><td>#{before}</td><td>#{after}#{afterhtml}</td></tr></table>"
end


####
# tag kdlink
website.config.define_option('tag.kdlink.oid', nil)
website.config.define_option('tag.kdlink.part', nil)
website.ext.tag.register('kdlink', :mandatory => ['oid', 'part'],
                         :config_prefix => 'tag.kdlink') do |tag, body, context|
  link = "<a href='syntax.html##{context[:config]['tag.kdlink.oid']}'>&rarr; Syntax for #{context[:config]['tag.kdlink.part']}</a>"
  "<div class='kdsyntaxlink'>#{link}\n</div>"
end
